package controllers

import "net/http"

// FileController 文件处理
type FileController struct {
	BaseController
}

// Constructor 初始化 Controller
// @Title Constructor
// @Description 初始化 Controller, 系统自动调用
func (c *FileController) Constructor() {}

// UploadImage Upload image to ali-oss
// @Title Upload image to ali-oss
// @Description 上传图片到阿里云
// @Param   UpImg     form    string  true        "图片控件name"
// @Success 200 { Url } 图片URL
// @Failure >300 error message
func (c *FileController) UploadImage() {
	imgURL, err := c.UploadFileToAliOSS("UpImg")
	if err != nil {
		c.ResponseJson(err, http.StatusInternalServerError)
	}

	imageSuffix := c.AliConf["oss::ImageSuffix"]

	resp := map[string]interface{}{
		"Url": imgURL + imageSuffix,
	}

	c.ResponseJson(resp)
}

// UploadBase64Image Upload base64-image to ali-oss
// @Title Upload image to ali-oss
// @Description 上传base64图片到阿里云
// @Param   UpImgStr     form    string  true        "图片控件name"
// @Success 200 { Url } 图片URL
// @Failure >300 error message
func (c *FileController) UploadBase64Image() {
	base64Str := c.GetString("UpImgStr")

	imgURL, err := c.UploadStringToAliOSS(base64Str)
	if err != nil {
		c.ResponseJson(err, http.StatusInternalServerError)
	}

	imageSuffix := c.AliConf["oss::ImageSuffix"]

	resp := map[string]interface{}{
		"Url": imgURL + imageSuffix,
	}

	c.ResponseJson(resp)
}
